﻿Imports Microsoft.TeamFoundation.WorkItemTracking.Client
Imports Microsoft.TeamFoundation.WorkItemTracking.Controls
Imports Microsoft.TeamFoundation.Client
Imports Microsoft.TeamFoundation.Server

'Imports Microsoft.VisualStudio.TeamFoundation.WorkItemTracking

Namespace SignOff

    Partial Public Class SignOffControl
        Implements IWorkItemControl


#Region " IWorkItemControl "

        Public Event AfterUpdateDatasource(ByVal sender As Object, ByVal e As System.EventArgs) Implements Microsoft.TeamFoundation.WorkItemTracking.Controls.IWorkItemControl.AfterUpdateDatasource
        Public Event BeforeUpdateDatasource(ByVal sender As Object, ByVal e As System.EventArgs) Implements Microsoft.TeamFoundation.WorkItemTracking.Controls.IWorkItemControl.BeforeUpdateDatasource

        Protected m_serviceProvider As IServiceProvider = Nothing
        Private m_securityService As IGroupSecurityService = Nothing
        Private m_WorkItemStore As WorkItemStore = Nothing
        Protected m_workItem As WorkItem = Nothing
        Protected m_workItemFieldName As String = Nothing
        Protected m_properties As System.Collections.Specialized.StringDictionary


        Public Property [ReadOnly]() As Boolean Implements Microsoft.TeamFoundation.WorkItemTracking.Controls.IWorkItemControl.ReadOnly
            Get
                Return Not Me.IsEnabled
            End Get
            Set(ByVal value As Boolean)
                Me.IsEnabled = Not value
            End Set
        End Property

        Public Sub SetSite(ByVal serviceProvider As System.IServiceProvider) Implements Microsoft.TeamFoundation.WorkItemTracking.Controls.IWorkItemControl.SetSite
            Try
                m_serviceProvider = serviceProvider
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try

        End Sub

        Public Property WorkItemDatasource() As Object Implements Microsoft.TeamFoundation.WorkItemTracking.Controls.IWorkItemControl.WorkItemDatasource
            Get
                Return m_workItem
            End Get
            Set(ByVal value As Object)
                If value Is Nothing And Not m_workItem Is Nothing Then
                    RemoveHandler m_workItem.FieldChanged, AddressOf OnFieldChanged
                End If
                m_workItem = value
                OnWorkItemChanged()
                If Not m_workItem Is Nothing Then
                    AddHandler m_workItem.FieldChanged, New WorkItemFieldChangeEventHandler(AddressOf OnFieldChanged)
                End If
            End Set
        End Property

        Public Property WorkItemFieldName() As String Implements Microsoft.TeamFoundation.WorkItemTracking.Controls.IWorkItemControl.WorkItemFieldName
            Get
                Return m_workItemFieldName
            End Get
            Set(ByVal value As String)
                m_workItemFieldName = value
            End Set
        End Property

        Public Property Properties() As System.Collections.Specialized.StringDictionary Implements Microsoft.TeamFoundation.WorkItemTracking.Controls.IWorkItemControl.Properties
            Get
                Return m_properties
            End Get
            Set(ByVal value As System.Collections.Specialized.StringDictionary)
                m_properties = value
            End Set
        End Property

        Public Sub Clear() Implements Microsoft.TeamFoundation.WorkItemTracking.Controls.IWorkItemControl.Clear

        End Sub

        Public Sub FlushToDatasource() Implements Microsoft.TeamFoundation.WorkItemTracking.Controls.IWorkItemControl.FlushToDatasource
            Try
                RaiseEvent BeforeUpdateDatasource(Me, EventArgs.Empty)
                m_workItem.Fields(WorkItemFieldName).Value = ux_Value.Text
                RaiseEvent AfterUpdateDatasource(Me, EventArgs.Empty)
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try

        End Sub



        Public Sub InvalidateDatasource() Implements Microsoft.TeamFoundation.WorkItemTracking.Controls.IWorkItemControl.InvalidateDatasource
            Try
                If Not m_workItem Is Nothing Then
                    Me.DataContext = m_workItem.Fields(WorkItemFieldName)
                    'Check constraints
                    If m_workItem.Fields(WorkItemFieldName).HasAllowedValuesList Then
                        If m_workItem.Fields(WorkItemFieldName).AllowedValues.Contains(m_workItem.Project.Store.TeamFoundationServer.AuthenticatedUserDisplayName) Then
                            Me.IsEnabled = True
                        Else
                            Me.IsEnabled = False
                        End If
                    End If
                End If
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try

        End Sub

#End Region


        Protected Sub OnFieldChanged(ByVal sender As Object, ByVal e As WorkItemEventArgs)

        End Sub

        Private Sub OnWorkItemChanged()
            If Not String.IsNullOrEmpty(m_workItem.Fields(WorkItemFieldName).Value) Then
                uxAction.Content = "Clear"
            Else
                uxAction.Content = "Sign Off"
            End If
        End Sub

        Private Sub uxAction_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles uxAction.Click
            Try
                If String.IsNullOrEmpty(ux_Value.Text) Then
                    ux_Value.Text = m_workItem.Project.Store.TeamFoundationServer.AuthenticatedUserDisplayName
                    uxAction.Content = "Clear"
                Else
                    ux_Value.Text = ""
                    uxAction.Content = "Sign Off"
                End If
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try


        End Sub


    End Class

End Namespace